﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Enemy.CommonEnemy;
using Assets.Scripts.DL.Mgrs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Assets.Scripts.DL.Enemy.SpriderBoss {
	public class SpiderBossJumpFallAtk : CommonState<SpiderBossController> {
		readonly Timer _timer;
		bool _sendDamage;
		float _range;
		readonly int _damage;
		float _jumpFinalX;//跳跃的最终x坐标
		float _jumpFinalY;//跳跃的高度
		bool _isJump;
		bool _isFall;
		bool _isOngroundAnimPlaying;
		readonly Timer _sendDmgTimer = new();
		public SpiderBossJumpFallAtk(SpiderBossController controller, StateEnum stateName) : base(controller, stateName) {
			_timer = _controller.StateData.AtkCdRecoder[StateName];

			_jumpFinalY = _controller.MaxJumpHeight;
			AddConditionToSelf(() => _timer.Elapsed > _controller.BarrageCd);
		}

		//跳跃的方向和
		public override void Enter() {
			_controller.SetGravity(0);
			_sendDamage = false;
			_controller.Velocity = Vector2.zero;
			_controller.PlayAnim(StateEnum.ReadyJump);
			_range = _controller.Atk3Range;
			_jumpFinalY = _controller.MaxJumpHeight + _controller.transform.position.y;
			_isJump = false;
			_isFall = false;
			_isOngroundAnimPlaying = false;
			_sendDmgTimer.Reset();
		}

		public override void Exit() {
			_controller.ResetGravity();
			_timer.Reset();
		}

		public override void Update() {

			if (_controller.IsAnimPlayAt(_controller.JumpNormalTime) && !_isJump) {
				_jumpFinalX = _controller.GetPlayerPos().x;
				var curpos = _controller.transform.position;
				var jumpDir = new Vector2(_jumpFinalX - curpos.x, _jumpFinalY - curpos.y).normalized;
				_controller.Velocity = jumpDir * _controller.JumpSpeed;
				_isJump = true;
				//Debug.Log($"{_jumpFinalX},{_jumpFinalY}");
				_controller.PlayAnim(StateEnum.Jump);
			}
			//Debug.Log(_controller.transform.position.y);
			if (_isJump && _controller.transform.position.y > _jumpFinalY && !_isFall) {
				_controller.Velocity = new Vector2(0, -1 * _controller.FallSpeed);
				_isFall = true;
				_controller.PlayAnim(StateEnum.Fall);
			}
			var tmp = true;
			if (_isFall && _controller.IsOnGround() && !_isOngroundAnimPlaying) {
				_controller.PlayAnim(StateEnum.FallOnGround);
				AudioManager.Play(_controller.StateAudio, AudioStr.FallOnGround);
				_isOngroundAnimPlaying = true;
				tmp = false;
				var pos = (Vector3)_controller.DICIOffset + _controller.transform.position;
				_controller.StartCoroutine(PoolTools.MakeDiciCoroutin(pos, _controller.DiciDisGap, 10, _controller.DiciTimeGap));
				CameraImpulseMgr.Instance.Generate(Vector2.down);
			}

			if (_isOngroundAnimPlaying && tmp) {
				if (_controller.IsAnimPlayOver()) {
					_controller.StateData.AtkOver = true;
				}
			}
			//跳起之后碰到就施加伤害，每次伤害间隔0.5
			if (_isJump && IsTouchPlayer() && _sendDmgTimer.Elapsed > 0.5f) {
				var atkOff = _controller.Atk3Off;
				if (!_isOngroundAnimPlaying) {
					atkOff = _controller.Atk3GroundOff;
				}
				_controller.CheckAtk(atkOff, _range, _damage);

				_sendDmgTimer.Reset();
			}
		}

		bool IsTouchPlayer()
			=> null != Physics2D.OverlapCircle(_controller.transform.position, _range, 1 << LayerMask.NameToLayer(LayerStrings.Player));
	}
}
